//
//  LevelScene.cpp
//  BayMaxPuzzle
//
//  Created by Jacky on 15/3/28.
//
//

#include "MainScene.h"
#include "LevelScene.h"
#include "GameScene.h"

USING_NS_CC;
using namespace ui;

Scene* MainScene::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = MainScene::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool MainScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    //bk
    auto mainbk = Sprite::create("main.png");
    mainbk->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
    mainbk->setScale(getRealScale(mainbk->getContentSize().width));
    this->addChild(mainbk);
    
    //title
    Sprite* dabailale[4];
    dabailale[0] = Sprite::create("da.png");
    dabailale[1] = Sprite::create("bai.png");
    dabailale[2] = Sprite::create("lai.png");
    dabailale[3] = Sprite::create("le.png");
    dabailale[0]->setPosition(Vec2(250,450));
    dabailale[1]->setPosition(Vec2(330,500));
    dabailale[2]->setPosition(Vec2(410,450));
    dabailale[3]->setPosition(Vec2(490,500));
    
    for (int i=0; i<4; i++) {
        dabailale[i]->setScale(1.2);
        this->addChild(dabailale[i]);
        dabailale[i]->runAction(RepeatForever::create(Sequence::create(
                                                 EaseSineOut::create(ScaleBy::create(0.8, 1.1)),
                                                 EaseSineOut::create(ScaleBy::create(0.6, 0.9)),
                                                 nullptr
                                                 )));
    }
    
    
    //eventlistener
    auto listener = EventListenerTouchOneByOne::create();
    
    listener->onTouchBegan = [=](Touch *touch,Event *event){
        AUDIOENGINE_PLAY_BUTTON_FX;
        Director::getInstance()->replaceScene(TransitionFade::create(0.5,LevelScene::createScene(), Color3B(255,255,255)));
        return true;
    };
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
    
    auto label_start = Label::createWithSystemFont("点 击 开 始", "Arial", 30);
    label_start->setPosition(Vec2(visibleSize.width/2, 100));
    this->addChild(label_start);
    label_start->runAction(RepeatForever::create(Sequence::create(FadeIn::create(0.5), FadeOut::create(0.5), nullptr)));
    
    AUDIOENGINE_PLAY_BK_MUSIC;
    
    return true;
}

float MainScene::getRealScale(int picWidth)
{
    float scale = 0;
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Size designSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
    float originScale = visibleSize.height/designSize.height;
    picWidth *= originScale;
    if(picWidth>visibleSize.width)
    {
        scale = 1;
    }
    else{
        scale = visibleSize.width/picWidth;
    }
    return scale;
}